<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div></div>
    <script>
      // 商店
      function JewelryStore(uname, address, list) {
        this.uname = uname
        this.address = address
        this.list = list||[]
      }
      // 商品
      function Product(shoppingName, price) {
        ;(this.shoppingName = shoppingName), (this.price = price)
      }
      // 实例化商品
      const fish = new Product('鱼', 5)
      const egg = new Product('鸡蛋', 7)
      const pig = new Product('猪', 10)
      const bird = new Product('鸟', 10)

      // 增
      JewelryStore.prototype.addProduct = function (product) {
        this.list.forEach(item=>{
          if(!item.shoppingName||!item.price){
            throw new Error('商品信息不完整')
          }
        })
        this.list.push(product)
  
        return this
      }
      // 删除
      JewelryStore.prototype.removeProduct=function(product){
       
      const index=this.list.findIndex(item=>item.shoppingName===product)
        if(index===-1){
          throw new Error('商品不存在')
        }
      
      this.list.splice(index,1)
      return this

     }
    //  求和
     JewelryStore.prototype.calculateTotalSales=function(){
        return this.list.reduce((sum,item)=>sum+item.price,0)
     }
     JewelryStore.prototype.listProducts=function(){
      console.log(this);
     }
     const store = new JewelryStore('阿新的店', '浙江杭州', )
      
     store.addProduct(fish)
     store.addProduct(egg)
     store.addProduct(pig)
     store.addProduct(bird)
    //  store.removeProduct('鸟')
     store.listProducts()
    //  store.removeProduct("猪头")
  
    
    //  console.log(store);
    // console.log( store.calculateTotalSales());
   
    
     
     
    </script>
  </body>
</html>
